﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using Maticsoft.DBUtility;
using KF.DHLModel;
using KF.Common;//请先添加引用

namespace KF.DHL.DAL
{
    public class Customer
    {
        public void Add(KF.DHLModel.BaseInfo.Customer customer)
        {
            String sql = @"Insert into Customers (Code,CName,Address,PhoneNumber,Email,Notes,CustomerType)
                            Values (@Code,@CName,@Address,@PhoneNumber,@Email,@Notes,@CustomerType)";
            OleDbParameter[] parameters = {
					new OleDbParameter("@Code", OleDbType.VarChar),
					new OleDbParameter("@CName", OleDbType.VarChar,255),
					new OleDbParameter("@Address", OleDbType.VarChar,255),
					new OleDbParameter("@PhoneNumber", OleDbType.VarChar,255),
					new OleDbParameter("@Email", OleDbType.VarChar,255),
					new OleDbParameter("@Notes", OleDbType.VarChar,255),
					new OleDbParameter("@CustomerType", OleDbType.Integer,4)};

            parameters[0].Value = customer.Code;
            parameters[1].Value = customer.CName;
            parameters[2].Value = customer.Address;
            parameters[3].Value = customer.PhoneNumber;

            parameters[4].Value = customer.Email;
            parameters[5].Value = customer.Notes;
            parameters[6].Value = (int)customer.Type;

            DbHelperOleDb.ExecuteSql(sql.ToString(), parameters);
        }

        public List<KF.DHLModel.BaseInfo.Customer> GetALL()
        {
            String sql = @"Select * from Customers";
            DataSet ds = new DataSet();

            try
            {
                ds = DbHelperOleDb.Query(sql);


                List<KF.DHLModel.BaseInfo.Customer> infos = new List<DHLModel.BaseInfo.Customer>();


                KF.DHLModel.BaseInfo.Customer info;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    info = new KF.DHLModel.BaseInfo.Customer();
                    info.Code = Convert.ToString(dr["Code"]);
                    info.CName = Convert.ToString(dr["CName"]);
                    info.Address = Convert.ToString(dr["Address"]);
                    info.PhoneNumber = Convert.ToString(dr["PhoneNumber"]);
                    info.Email = Convert.ToString(dr["Email"]);
                    info.Notes = Convert.ToString(dr["Notes"]);
                    info.Type = (CustomerType)Convert.ToInt32(dr["CustomerType"]);
                    infos.Add(info);
                }
                return infos;

            }
            catch (System.Exception ex)
            {
                LogHelper.DebugHelper.WriteLog(ex.ToString(), LogHelper.ExceptionHelper.ExceptionLevel.Exception);
                throw (ex);
            }
        }


        public void Update(KF.DHLModel.BaseInfo.Customer customer)
        {
            String sql = @"update Customers set CName = @CName,
                                                Address = @Address,
                                                PhoneNumber = @Phone,
                                                Email = @Email,
                                                Notes = @Notes,
                                                CustomerType = @CustomerType
                            where Code = '"+customer.Code+"'";
            OleDbParameter[] parameters = {
					new OleDbParameter("@CName", OleDbType.VarChar,255),
					new OleDbParameter("@Address", OleDbType.VarChar,255),
					new OleDbParameter("@PhoneNumber", OleDbType.VarChar,255),
					new OleDbParameter("@Email", OleDbType.VarChar,255),
					new OleDbParameter("@Notes", OleDbType.VarChar,255),
					new OleDbParameter("@CustomerType", OleDbType.Integer,4)};

            parameters[0].Value = customer.CName;
            parameters[1].Value = customer.Address;
            parameters[2].Value = customer.PhoneNumber;

            parameters[3].Value = customer.Email;
            parameters[4].Value = customer.Notes;
            parameters[5].Value = (int)customer.Type;

            DbHelperOleDb.ExecuteSql(sql.ToString(), parameters);
        }
    }
}
